package com.myzhouye.starter.ticket.service.impl;/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

import com.myzhouye.starter.ticket.exception.TicketExpireException;
import com.myzhouye.starter.ticket.properties.TicketProperties;
import com.myzhouye.starter.ticket.service.IticketUtilService;
import com.myzhouye.starter.ticket.utils.MD5Util;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @program: ticket-spring-boot-starter
 * @description:
 * @author: 那条蠢鱼
 * @create: 2024-11-14 16:31
 **/
public class DefaultTicketUtilService implements IticketUtilService {
    private RedisTemplate redisTemplate;
    private TicketProperties ticketProperties;

    public DefaultTicketUtilService(RedisTemplate redisTemplate, TicketProperties ticketProperties) {
        this.redisTemplate = redisTemplate;
        this.ticketProperties = ticketProperties;
    }

    @Override
    public String createTicket(Object obj) {
        Random random=new Random();
        String taskIdMockFmt="%s%02d";
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String ticketStr = String.format(taskIdMockFmt, LocalDateTime.now().format(dtf), random.nextInt(90) + 10);
        String s = MD5Util.encryptMD5(ticketStr);
        redisTemplate.opsForValue().set(s,obj,ticketProperties.getTicketAliveTime(), TimeUnit.MILLISECONDS);
        return s;
    }

    @Override
    public String createTicket(Object obj, long time) {
        Random random=new Random();
        String taskIdMockFmt="%s%02d";
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String ticketStr = String.format(taskIdMockFmt, LocalDateTime.now().format(dtf), random.nextInt(90) + 10);
        String s = MD5Util.encryptMD5(ticketStr);
        redisTemplate.opsForValue().set(s,obj,time, TimeUnit.MILLISECONDS);
        return s;
    }

    @Override
    public Object checkTicket(String ticket) throws TicketExpireException {
        Object o = redisTemplate.opsForValue().getAndDelete(ticket);
        if(ObjectUtils.isEmpty(o)){
            throw new TicketExpireException();
        }
        return o;
    }
}
